package day9.guanlixitong.src.ui;

import day9.guanlixitong.src.bean.Employee;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddEmployeeFrame extends JFrame implements ActionListener {

    private JTextField idField; // ID输入框
    private JTextField nameField; // 姓名输入框
    private JTextField departmentField; // 部门输入框
    private JTextField positionField; // 职位输入框
    private JTextField salaryField; // 薪水输入框
    private JButton addButton; // 添加按钮
    private JButton cancelButton; // 取消按钮
    private  EmployeeManager  EmployeeManagerUI;//管理界面
//    public static void main(String[] args) {
//        // 确保Swing组件在事件调度线程上创建
//       new AddEmployeeFrame();
//    }

    public AddEmployeeFrame(EmployeeManager Em) {
        setTitle("添加员工");
        setSize(400, 300); // 设置窗口大小
        setLocationRelativeTo(null); // 居中显示
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //拿到原本的管理页面，用于回调方便
        this.EmployeeManagerUI=Em;
        // 设置内容面板的布局为BorderLayout
        setLayout(new BorderLayout());

        // 创建一个面板来放置注册表单
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(6, 1, 10, 10)); // 6行1列布局
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 内边距
        formPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // 居中对齐

        // 添加ID标签和文本框
        JLabel idLabel = new JLabel("ID:", SwingConstants.RIGHT);
        idLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(idLabel);

        idField = new JTextField();
        idField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(idField);

        // 添加姓名标签和文本框
        JLabel nameLabel = new JLabel("姓名:", SwingConstants.RIGHT);
        nameLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(nameLabel);

        nameField = new JTextField();
        nameField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(nameField);

        // 添加部门标签和文本框
        JLabel departmentLabel = new JLabel("部门:", SwingConstants.RIGHT);
        departmentLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(departmentLabel);

        departmentField = new JTextField();
        departmentField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(departmentField);

        // 添加职位标签和文本框
        JLabel positionLabel = new JLabel("职位:", SwingConstants.RIGHT);
        positionLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(positionLabel);

        positionField = new JTextField();
        positionField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(positionField);

        // 添加薪水标签和文本框
        JLabel salaryLabel = new JLabel("薪水:", SwingConstants.RIGHT);
        salaryLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(salaryLabel);

        salaryField = new JTextField();
        salaryField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(salaryField);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // 居中对齐

        // 添加添加按钮
        addButton = createStyledButton("添加");
        buttonPanel.add(addButton);
        addButton.addActionListener(this);

        // 添加取消按钮
        cancelButton = createStyledButton("取消");
        buttonPanel.add(cancelButton);
        cancelButton.addActionListener(this);

        // 将注册表单和按钮添加到主窗口
        add(formPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置背景颜色
        getContentPane().setBackground(new Color(240, 240, 240));

        // 设置可见性
        setVisible(true);
    }

    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setFont(new Font("楷体", Font.BOLD, 14));
        button.setBackground(new Color(0, 128, 255));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        button.setPreferredSize(new Dimension(120, 30)); // 设置按钮大小
        return button;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addButton) {
            addEmployee();
        } else  {
            this.dispose(); // 关闭当前窗口
        }
    }

    public void addEmployee() {
        String id = idField.getText();
        String name = nameField.getText();
        String department = departmentField.getText();
        String position = positionField.getText();
        String salaryStr = salaryField.getText();

        if (id.isEmpty() || name.isEmpty() || department.isEmpty() || position.isEmpty() || salaryStr.isEmpty()) {
            JOptionPane.showMessageDialog(this, "所有字段都不能为空！");
            return;
        }

        try {
            double salary = Double.parseDouble(salaryStr);
            // 这里可以添加保存员工信息的逻辑，例如将员工信息存储到数据库或列表中
            JOptionPane.showMessageDialog(this, "添加员工成功！\nID: " + id + "\n姓名: " + name + "\n部门: " + department + "\n职位: " + position + "\n薪水: " + salary);
            // 清空输入框
            idField.setText("");
            nameField.setText("");
            departmentField.setText("");
            positionField.setText("");
            salaryField.setText("");
            //EmployeeManager.employees.add(new Employee(Integer.parseInt(id),name,department,position,(int)salary));

            this.EmployeeManagerUI.addEmployeer(new Employee(Integer.parseInt(id),name,department,position,(int)salary));
            JOptionPane.showMessageDialog(this,"添加成功");
            this.dispose();
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "薪水必须是数字！");
        }
    }


}